<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="goods_id">
                            <option value="">商品名称</option>
                            {volist name="goods" id="vo"}
                            <option value="{$vo.id}">{$vo.title}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="is_deliver">
                            <option value="">订单状态</option>
                            <option value="1">新订单</option>
                            <option value="2">已发放</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="time" id="date" placeholder="时间（范围）" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="工号|姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="import">
        <i class="layui-icon layui-icon-release"></i>
        发货
    </button>
</script>

<script type="text/html" id="user-bar">
    {{#if (d.is_deliver == '0') { }}
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="release"><i class="layui-icon layui-icon-release"></i></button>
    {{# } }}
</script>
<script type="text/html" id="table-type">
    {{#if (d.goods_type == '1') { }}
    <span>实物</span>
    {{# }else if(d.goods_type == '2'){ }}
    <span>虚拟</span>
    {{# } }}
</script>

<script type="text/html" id="table-state">
    {{#if (d.is_deliver == '0') { }}
    <span style="color: red;">新订单</span>
    {{# }else if(d.is_deliver == '1'){ }}
    <span>已发放</span>
    {{# } }}
</script>

<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery','common', 'laydate'], function() {
        let table = layui.table;
        let form = layui.form;
        let laydate = layui.laydate;
        let $ = layui.jquery;
        let common = layui.common;


        laydate.render({
            elem: '#date'
            ,range: true //或 range: '~' 来自定义分割字符
            ,trigger: 'click'
            ,type: 'date'
        });

        let cols = [
            [{
                type: 'checkbox',
                fixed: 'left',
            },
                {
                    title: '订单号',
                    field: 'no',
                    align: 'center',
                },
                {
                    title: '姓名',
                    field: 'name',
                    align: 'center',
                },
                {
                    title: '工号',
                    field: 'userid',
                    align: 'center',
                },
                {
                    title: '兑换商品',
                    field: 'goods_name',
                    align: 'center',
                },
                {
                    title: '商品类型',
                    field: 'goods_type',
                    align: 'center',
                    templet:'#table-type',
                },
                {
                    title: '消耗积分',
                    field: 'integral',
                    align: 'center',
                },
                {
                    title: '状态',
                    field: 'is_deliver',
                    align: 'center',
                    templet:'#table-state',
                },
                {
                    title: '兑换时间',
                    field: 'add_time',
                    align: 'center',
                    templet: '#create-time'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Exchange/load',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }]
        });
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'release') {
                window.release(obj);
            }
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'import') {
                window.import();
            } else if (obj.event === 'export') {
                window.export();
            }
        });


        window.release = function(obj) {
            layer.open({
                type: 2,
                title: '发货',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Exchange/release?id='+obj.data.id
            });
        }

        // 数据导出
        window.export = function() {
            layer.confirm('确定要导出数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('export')}", "_blank");
            });
        };

        window.import = function() {
            layer.open({
                type: 2,
                title: '导入',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Exchange/import'
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
